package edu.hubu.service.impl;

import edu.hubu.commons.Object2List;
import edu.hubu.commons.Object2Map;
import edu.hubu.commons.ResultsJson;
import edu.hubu.commons.StatusCode;
import edu.hubu.dao.impl.IBookDao;
import edu.hubu.model.Book;
import edu.hubu.service.AbstractService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author moonlan
 * @date 2020/10/26 下午9:28
 */
public class BookService extends AbstractService<Book> {

    public BookService(IBookDao dao) {
        super(dao);
    }

    @Override
    public ResultsJson findByLikePage(int currentPage, int size, Map<String, Object> map) {
        ResultsJson page = super.findByLikePage(currentPage, size, map);
        List<Book> users = Object2List.toList(page.getData(), Book.class);
        List<Object> collect = users.stream().map(Object2Map::filterMap).collect(Collectors.toList());
        return ResultsJson.toJson(StatusCode.SUCCESS, page.getTotal(), page.getCurrent(), collect);
    }

    @Override
    public ResultsJson findAllPage(int currentPage, int size) {
        ResultsJson page = super.findAllPage(currentPage, size);
        List<Book> users = Object2List.toList(page.getData(), Book.class);
        List<Object> collect = users.stream().map(Object2Map::filterMap).collect(Collectors.toList());
        return ResultsJson.toJson(StatusCode.SUCCESS, page.getTotal(), page.getCurrent(), collect);
    }

    public ResultsJson updateById(String id, Book book) {
        return super.updateById(id, book, b->!b.isBookIsDeleted());
    }

    @Override
    public ResultsJson deleteById(String id) {
        return super.deleteById(id);
    }

    @Override
    public ResultsJson add(Book book) {
        return super.add(book);
    }

    public ResultsJson count() {
        return super.count("book");
    }
}
